package com.lyj.io.module.generator.service.impl;

import com.lyj.io.module.data.entity.TabFieldEntity;
import com.lyj.io.module.data.entity.TabsEntity;
import com.lyj.io.module.data.service.TabFieldService;
import com.lyj.io.module.data.service.TabsService;
import com.lyj.io.module.generator.service.SysGeneratorService;
import com.lyj.io.module.generator.utils.GenUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * 生成代码相关接口
 * @author:liyajie
 * @createTime:2021/12/30 17:07
 * @version:1.0
 */
@Service("SysGeneratorService")
public class SysGeneratorServiceImpl implements SysGeneratorService {

    @Autowired
    TabsService tabsService;

    @Autowired
    TabFieldService tabFieldService;


    @Override
    public byte[] generatorCode(String[] tableIds) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String id : tableIds) {
            // 查询表信息
            TabsEntity tabsEntity = tabsService.getById(id);
            // 查询列信息
            List<TabFieldEntity> tabFieldEntityList = tabFieldService.getByrelatedTabId(Long.parseLong(id));
            //生成代码
            GenUtils.generatorCode(tabsEntity, tabFieldEntityList, zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }
}
